package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.mapper.ErpProductBrandMapper;
import com.ruoyi.gather.api.domain.ErpProductBrand;
import com.ruoyi.gather.api.domain.dto.ErpBrandCountDto;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.gather.api.domain.ErpCollectProductExpand;
import com.erp.gather.domain.bo.ErpCollectProductExpandBo;
import com.erp.gather.domain.vo.ErpCollectProductExpandVo;
import com.erp.gather.mapper.ErpCollectProductExpandMapper;
import com.erp.gather.mapper.ErpCollectProductSkuMapper;
import com.erp.gather.service.IErpCollectProductExpandService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 采集商品拓展;参考AE-商品：发布产品信息（aliexpress.postproduct.redefining.postaeproduct）Service业务层处理
 *
 * @author erp
 * @date 2023-03-07
 */
@RequiredArgsConstructor
@Service
public class ErpCollectProductExpandServiceImpl implements IErpCollectProductExpandService {

    private final ErpCollectProductExpandMapper baseMapper;
    private final ErpCollectProductSkuMapper collectProductSkuMapper;
    private final ErpProductBrandMapper productBrandMapper;

    @Override
    public ErpCollectProductExpand selectByCollectProductId(Long collectProductId) {
        return baseMapper.selectOne(new LambdaQueryWrapper<ErpCollectProductExpand>().eq(ErpCollectProductExpand::getCollectProductId,collectProductId));
    }


    /**
     * 修改采集商品拓展;参考AE-商品：发布产品信息（aliexpress.postproduct.redefining.postaeproduct）
     */
    @Override
    public Boolean updateByBo(ErpCollectProductExpandBo bo) {
        ErpCollectProductExpand update = BeanUtil.toBean(bo, ErpCollectProductExpand.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }





    /**
     * 查询采集商品拓展;参考AE-商品：发布产品信息（aliexpress.postproduct.redefining.postaeproduct）列表
     */
    @Override
    public TableDataInfo<ErpCollectProductExpandVo> queryPageList(ErpCollectProductExpandBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpCollectProductExpand> lqw = buildQueryWrapper(bo);
        Page<ErpCollectProductExpandVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    private LambdaQueryWrapper<ErpCollectProductExpand> buildQueryWrapper(ErpCollectProductExpandBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpCollectProductExpand> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCollectProductId() != null, ErpCollectProductExpand::getCollectProductId, bo.getCollectProductId());
        lqw.eq(StringUtils.isNotBlank(bo.getAbsoluteQuoteMap()), ErpCollectProductExpand::getAbsoluteQuoteMap, bo.getAbsoluteQuoteMap());
        lqw.eq(StringUtils.isNotBlank(bo.getIsPackSell()), ErpCollectProductExpand::getIsPackSell, bo.getIsPackSell());
        lqw.eq(StringUtils.isNotBlank(bo.getBaseUnit()), ErpCollectProductExpand::getBaseUnit, bo.getBaseUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getAddUnit()), ErpCollectProductExpand::getAddUnit, bo.getAddUnit());
        lqw.eq(bo.getAddWeight() != null, ErpCollectProductExpand::getAddWeight, bo.getAddWeight());
        lqw.eq(bo.getBulkDiscount() != null, ErpCollectProductExpand::getBulkDiscount, bo.getBulkDiscount());
        lqw.eq(bo.getBulkOrder() != null, ErpCollectProductExpand::getBulkOrder, bo.getBulkOrder());
        lqw.eq(StringUtils.isNotBlank(bo.getCurrencyCode()), ErpCollectProductExpand::getCurrencyCode, bo.getCurrencyCode());
        lqw.eq(bo.getDeliveryTime() != null, ErpCollectProductExpand::getDeliveryTime, bo.getDeliveryTime());
        lqw.eq(StringUtils.isNotBlank(bo.getIsImageDynamic()), ErpCollectProductExpand::getIsImageDynamic, bo.getIsImageDynamic());
        lqw.eq(StringUtils.isNotBlank(bo.getKeyword()), ErpCollectProductExpand::getKeyword, bo.getKeyword());
        lqw.eq(StringUtils.isNotBlank(bo.getProductMoreKeywords1()), ErpCollectProductExpand::getProductMoreKeywords1, bo.getProductMoreKeywords1());
        lqw.eq(StringUtils.isNotBlank(bo.getProductMoreKeywords2()), ErpCollectProductExpand::getProductMoreKeywords2, bo.getProductMoreKeywords2());
        lqw.eq(bo.getLotNum() != null, ErpCollectProductExpand::getLotNum, bo.getLotNum());
        lqw.eq(StringUtils.isNotBlank(bo.getOwnerMemberId()), ErpCollectProductExpand::getOwnerMemberId, bo.getOwnerMemberId());
        lqw.eq(bo.getOwnerMemberSeq() != null, ErpCollectProductExpand::getOwnerMemberSeq, bo.getOwnerMemberSeq());
        lqw.eq(StringUtils.isNotBlank(bo.getPackageType()), ErpCollectProductExpand::getPackageType, bo.getPackageType());
        lqw.eq(bo.getStock() != null, ErpCollectProductExpand::getStock, bo.getStock());
        lqw.eq(StringUtils.isNotBlank(bo.getClassifyLabel()), ErpCollectProductExpand::getClassifyLabel, bo.getClassifyLabel());
        lqw.eq(StringUtils.isNotBlank(bo.getSummary()), ErpCollectProductExpand::getSummary, bo.getSummary());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpCollectProductExpand::getRevision, bo.getRevision());
        return lqw;
    }


    @Override
    public ErpCollectProductExpandVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }


    @Override
    public Boolean insertByBo(ErpCollectProductExpandBo bo) {
        ErpCollectProductExpand add = BeanUtil.toBean(bo, ErpCollectProductExpand.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;    }

    @Override
    public Boolean deleteWithValidByIds(List<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public R<Object> getCollectProductExpandInfo(ErpCollectProductExpand bo) {
        LambdaQueryWrapper<ErpCollectProductExpand> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCollectProductId() != null, ErpCollectProductExpand::getCollectProductId, bo.getCollectProductId());
        lqw.eq(StringUtils.isNotBlank(bo.getAbsoluteQuoteMap()), ErpCollectProductExpand::getAbsoluteQuoteMap, bo.getAbsoluteQuoteMap());
        lqw.eq(StringUtils.isNotBlank(bo.getIsPackSell()), ErpCollectProductExpand::getIsPackSell, bo.getIsPackSell());
        lqw.eq(StringUtils.isNotBlank(bo.getBaseUnit()), ErpCollectProductExpand::getBaseUnit, bo.getBaseUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getAddUnit()), ErpCollectProductExpand::getAddUnit, bo.getAddUnit());
        lqw.eq(bo.getAddWeight() != null, ErpCollectProductExpand::getAddWeight, bo.getAddWeight());
        lqw.eq(bo.getBulkDiscount() != null, ErpCollectProductExpand::getBulkDiscount, bo.getBulkDiscount());
        lqw.eq(bo.getBulkOrder() != null, ErpCollectProductExpand::getBulkOrder, bo.getBulkOrder());
        lqw.eq(StringUtils.isNotBlank(bo.getCurrencyCode()), ErpCollectProductExpand::getCurrencyCode, bo.getCurrencyCode());
        lqw.eq(bo.getDeliveryTime() != null, ErpCollectProductExpand::getDeliveryTime, bo.getDeliveryTime());
        lqw.eq(StringUtils.isNotBlank(bo.getIsImageDynamic()), ErpCollectProductExpand::getIsImageDynamic, bo.getIsImageDynamic());
        lqw.eq(StringUtils.isNotBlank(bo.getKeyword()), ErpCollectProductExpand::getKeyword, bo.getKeyword());
        lqw.eq(StringUtils.isNotBlank(bo.getProductMoreKeywords1()), ErpCollectProductExpand::getProductMoreKeywords1, bo.getProductMoreKeywords1());
        lqw.eq(StringUtils.isNotBlank(bo.getProductMoreKeywords2()), ErpCollectProductExpand::getProductMoreKeywords2, bo.getProductMoreKeywords2());
        lqw.eq(bo.getLotNum() != null, ErpCollectProductExpand::getLotNum, bo.getLotNum());
        lqw.eq(StringUtils.isNotBlank(bo.getOwnerMemberId()), ErpCollectProductExpand::getOwnerMemberId, bo.getOwnerMemberId());
        lqw.eq(bo.getOwnerMemberSeq() != null, ErpCollectProductExpand::getOwnerMemberSeq, bo.getOwnerMemberSeq());
        lqw.eq(StringUtils.isNotBlank(bo.getPackageType()), ErpCollectProductExpand::getPackageType, bo.getPackageType());
        lqw.eq(bo.getStock() != null, ErpCollectProductExpand::getStock, bo.getStock());
        lqw.eq(StringUtils.isNotBlank(bo.getClassifyLabel()), ErpCollectProductExpand::getClassifyLabel, bo.getClassifyLabel());
        lqw.eq(StringUtils.isNotBlank(bo.getSummary()), ErpCollectProductExpand::getSummary, bo.getSummary());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpCollectProductExpand::getRevision, bo.getRevision());

        ErpCollectProductExpand erpCollectProductExpandSel = baseMapper.selectOne(lqw);
        if (Objects.isNull(erpCollectProductExpandSel)){
            return R.fail("信息未查到");
        }else {
            return R.ok(erpCollectProductExpandSel);
        }
    }

    @Override
    public Boolean updateProductStatusById(Long id, Long collectProductId, Integer status) {
        return baseMapper.updateProductStatusById(id, collectProductId, status);
    }

    /**
     * @Description: 查询品牌ID和对应的商品数量
     * @Author: j.zh
     * @Date: 2024/7/4
     * @return
     * @Version: 4.2.0
     **/
    @Override
    public List<ErpBrandCountDto> getBrandAndProductCount(){
        List<ErpBrandCountDto> dtoList = baseMapper.selectBrandAndProductCount();
        ErpProductBrand productBrand = null;
        List<ErpBrandCountDto> newDtoList = new ArrayList<>();
        // 根据brandId查询详细信息
        for (ErpBrandCountDto dto : dtoList){
            productBrand = productBrandMapper.selectById(dto.getBrandId());
            BeanUtils.copyProperties(productBrand, dto);
            newDtoList.add(dto);
        }
        return newDtoList;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpCollectProductExpand entity){
        //TODO 做一些数据校验,如唯一约束
    }

}
